﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Model;

namespace Utils
{
    public class DatatypeConfig
    {
        private List<KeyValue> dbTypeLst;
        public DatatypeConfig()
        {
            dbTypeLst = GetDbToConfig();
        }
        public List<KeyValue> GetDbToConfig()
        {
            string xmlpath = FileHelper.RootPath() + "/config/DatatypeMap.xml";
            XElement root = XElement.Load(xmlpath);
            XElement dbto = root.Element(SetConfig.GetDataType());
            List<KeyValue> lst = new List<KeyValue>();
            if (dbto.HasElements)
            {
                IEnumerable<XElement> xElements = dbto.Elements();

                foreach (var xElement in xElements)
                {
                    KeyValue model = new KeyValue
                                         {
                                             Key = xElement.Attribute("key").Value,
                                             Value = xElement.Attribute("value").Value
                                         };
                    lst.Add(model);
                }
            }

            return lst;
        }
        /// <summary>
        /// 获取数据库数据类型对应的Java数据类型
        /// </summary>
        /// <param name="dbType">数据库数据类型</param>
        /// <returns></returns>
        public string GetDataType(string dbType)
        {
            var firstOrDefault = dbTypeLst.FirstOrDefault(o => o.Key.ToLower() == dbType.ToLower());
            if (firstOrDefault != null)
                return firstOrDefault.Value;
            return "String";
        }
    }
}
